package com.pro.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pro.back.bean.*;
import com.pro.back.bean.bo.CountBO;
import com.pro.back.bean.bo.CountBO2;
import com.pro.back.bean.bo.UserBO;
import com.pro.back.bean.common.ApiRes;
import com.pro.back.bean.po.UserPO;
import com.pro.back.dao.BorrowMapper;
import com.pro.back.dao.DeviceMapper;
import com.pro.back.dao.DuserMapper;
import com.pro.back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: jingwenjuan
 * @Description:
 * @Date: 2025-04-19
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private DuserMapper duserMapper;
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 用户登录
     * 1. 根据编号和密码登录
     * 2. 根据编号和密码查询数据库是否有该用户信息
     * 3. 没有则返回登陆失败，有的话则查询当前登录用户的详细信息
     * @param duser
     * @return
     */
    @Override
    public ApiRes login(Duser duser) {
        DuserExample ex = new DuserExample();
        ex.createCriteria().andUsercodeEqualTo(duser.getUsercode()).andPasswordEqualTo(duser.getPassword());
        List<Duser> dusers = duserMapper.selectByExample(ex);
        if (dusers!=null && dusers.size()>0) {
            UserBO userBO = duserMapper.selectUserInfo(dusers.get(0).getId());
            return new ApiRes(userBO);
        } else {
            return new ApiRes(500,null,"账号不存在或密码错误！");
        }
    }

    /**
     * 用户注册
     * 1. 校验数据库是否存在相同编号的用户
     * 2. 存在的话则不允许添加，提示改变好的用户已经存在，添加失败
     * 3. 不存在的话则添加成功
     * @param duser
     * @return
     */
    @Override
    public ApiRes register(Duser duser) {
        DuserExample example = new DuserExample();

        if (duser.getId()!=null) {//编辑
            example.createCriteria().andUsercodeEqualTo(duser.getUsercode()).andIdNotEqualTo(duser.getId());
            List<Duser> users = duserMapper.selectByExample(example);
            if (users!=null && users.size()>0) {
                return new ApiRes(500,null,"用户名已经被注册，请重试!");
            }
            duserMapper.updateByPrimaryKeySelective(duser);
        } else {
            example.createCriteria().andUsercodeEqualTo(duser.getUsercode());
            List<Duser> users = duserMapper.selectByExample(example);
            if (users!=null && users.size()>0) {
                return new ApiRes(500,null,"用户名已经被注册，请重试!");
            }
            duserMapper.insertSelective(duser);
        }

        return login(duserMapper.selectUserInfo(duser.getId()));
    }

    /**
     * 删除用户
     * 1. 查询该用户有没有没有归还的设备，有的话暂时不能删除，没有的话直接删除
     */
    @Override
    public ApiRes delUser(Long id) {
        BorrowExample ex = new BorrowExample();
        ex.createCriteria().andUserIdEqualTo(id).andStatusEqualTo("0");
        List<Borrow> list = borrowMapper.selectByExample(ex);
        if (list!=null && list.size()>0) {
            return new ApiRes(500,null,"该用户有尚未归还的设备，暂时不能删除！");
        }
        duserMapper.deleteByPrimaryKey(id);
        return new ApiRes("success");
    }

    /**
     * 分页条件查询
     */
    @Override
    public ApiRes selectAll(UserPO po) {
        PageHelper.startPage(po.getPageNum(),po.getPageSize());
        List<UserBO> list = duserMapper.selectAllUser(po);
        PageInfo<UserBO> info = new PageInfo<>(list);
        return new ApiRes(info);
    }

    /**
     * 设备正常状态、已借出状态统计
     * 查询device表，分别统计status为0和status为1 的个数
     */
    @Override
    public ApiRes deviceBorrowStatus() {
        List<CountBO> list = new ArrayList<>();

        DeviceExample example = new DeviceExample();
        example.createCriteria().andBorrowStatusEqualTo("0");
        long l1 = deviceMapper.countByExample(example);
        list.add(new CountBO("正常",l1));

        example.clear();
        example.createCriteria().andBorrowStatusEqualTo("1");
        long l2 = deviceMapper.countByExample(example);
        list.add(new CountBO("借出中",l2));

        return new ApiRes(list);
    }

    /**
     * 设备修理状态统计
     * 统计设备 正常 报修、检修、报废的个数
     */
    @Override
    public ApiRes deviceRepairStatus() {
        List<CountBO> list = new ArrayList<>();

        DeviceExample example = new DeviceExample();
        example.createCriteria().andRepairStatusEqualTo("0");
        long l1 = deviceMapper.countByExample(example);
        list.add(new CountBO("正常",l1));

        example.clear();
        example.createCriteria().andRepairStatusEqualTo("1");
        long l2 = deviceMapper.countByExample(example);
        list.add(new CountBO("报修中",l2));

        example.clear();
        example.createCriteria().andRepairStatusEqualTo("2");
        long l3 = deviceMapper.countByExample(example);
        list.add(new CountBO("检修中",l3));

        example.clear();
        example.createCriteria().andRepairStatusEqualTo("3");
        long l4 = deviceMapper.countByExample(example);
        list.add(new CountBO("报废",l4));

        return new ApiRes(list);
    }

    /**
     * 查询人数最多的6个班级
     * 1. 查询人数最多的6个班级的班级名称
     * 2. 查询每个班级的人数
     * @return
     */
    @Override
    public ApiRes peopleNumCount() {
        PageHelper.startPage(1,6);
        List<CountBO> countBOS = duserMapper.selectUserCount();
        List<String> names = countBOS.stream().map(CountBO::getName).collect(Collectors.toList());
        List<Long> values = countBOS.stream().map(CountBO::getValue).collect(Collectors.toList());

        CountBO2 bo2 = new CountBO2();
        bo2.setNames(names);
        bo2.setValues(values);
        return new ApiRes(bo2);
    }

    /**
     * 查询分类下设备最多的6个分类
     * @return
     */
    @Override
    public ApiRes deviceCount() {
        PageHelper.startPage(1,4);
        List<CountBO> countBOS = deviceMapper.selectDeviceCount();
        List<String> names = countBOS.stream().map(CountBO::getName).collect(Collectors.toList());
        List<Long> values = countBOS.stream().map(CountBO::getValue).collect(Collectors.toList());

        CountBO2 bo2 = new CountBO2();
        bo2.setNames(names);
        bo2.setValues(values);
        return new ApiRes(bo2);
    }
}
